package core;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

public class Peer {

    private String ip;
    private String id;
    private int port;
    private Socket socket;
    private OutputStream output;
    private InputStream input;
    private boolean choked;
    private boolean is_choked;
    private boolean interested;
    private boolean is_interested;
    private String peerId;
    private byte[] infoHash;
    private byte[] bitfield;

    public Peer(String ip, String id, int port) throws PeerConnectException {
        this.choked = true;
        this.interested = false;
        this.is_choked = true;
        this.is_interested = false;

        this.socket = new Socket();
        this.ip = ip;
        this.port = port;
        this.id = id;
         this.bitfield = null;
         

        try {
            this.socket.connect(new InetSocketAddress(this.ip, this.port));
            this.output = this.socket.getOutputStream();
            this.input = this.socket.getInputStream();
            
        } catch (IOException e) {
            throw new PeerConnectException();
        }
    }

    public byte[] getBitfield() {
        return bitfield;
    }

    public Peer(Socket socket) {
        this.choked = true;
        this.interested = false;
        this.is_choked = true;
        this.is_interested = false;

        this.socket = socket;
        this.ip = socket.getInetAddress().getHostAddress();
        this.port = socket.getPort();
        this.id = "";

        try {
            this.output = this.socket.getOutputStream();
            this.input = this.socket.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public void close()  {
    	try {
			this.output.close();			
	    	this.input.close();
	    	this.socket.close();
    	
    	} catch (IOException e) {
			e.printStackTrace();
		}
    }

    public boolean isChoked() {
        return choked;
    }

    public void setChoked(boolean choked) {
        this.choked = choked;
    }
    
    public boolean isIs_choked() {
        return is_choked;
    }

    public void setIs_choked(boolean isChoked) {
        is_choked = isChoked;
    }

    public void setBitfield(byte[] bitfield) {
        this.bitfield = bitfield;
    }

    public boolean isInterested() {
        return interested;
    }

    public void setInterested(boolean interested) {
        this.interested = interested;
    }

    public boolean isIs_interested() {
        return is_interested;
    }

    public void setIs_interested(boolean isInterested) {
        is_interested = isInterested;
    }

    public void setInput(InputStream input) {
        this.input = input;
    }

    public OutputStream getOutput() {
        return output;
    }

    public InputStream getInput() {
        return input;
    }

    public Peer(String ip, int port) throws PeerConnectException {
        this(ip, "", port);
    }

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getPeerId() {
        return peerId;
    }

    public void setPeerId(String peerId) {
        this.peerId = peerId;
    }

    public byte[] getInfoHash() {
        return infoHash;
    }

    public void setInfoHash(byte[] infoHash) {
        this.infoHash = infoHash;
        this.bitfield = new byte[TorrentManager.getInstance().getTorrent(infoHash).getBitfield().length];
    }

    @Override
    public String toString() {
        return "Peer [id=" + id + ", ip=" + ip + ", port=" + port + ", socket="
                + socket + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((ip == null) ? 0 : ip.hashCode());
        result = prime * result + port;
        result = prime * result + ((socket == null) ? 0 : socket.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Peer other = (Peer) obj;
        if (id == null) {
            if (other.id != null) {
                return false;
            }
        } else if (!id.equals(other.id)) {
            return false;
        }
        if (ip == null) {
            if (other.ip != null) {
                return false;
            }
        } else if (!ip.equals(other.ip)) {
            return false;
        }
        if (port != other.port) {
            return false;
        }
        if (socket == null) {
            if (other.socket != null) {
                return false;
            }
        } else if (!socket.equals(other.socket)) {
            return false;
        }
        return true;
    }
}
